﻿namespace SEED.SEEDViews {
    using System;
    using SEEDControllers;
    using System.Windows.Forms;
    using System.Drawing;

    public partial class SEEDataReader : Form {

        public SEEDataReader() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        public void DrawWaveForm(float[] data) {

            Bitmap bmp;
            if (this.pictureBox1.Image == null) {
                bmp = new Bitmap(970, 454);
            }
            else {
                bmp = (Bitmap)this.pictureBox1.Image;
            }

            using (Graphics g = Graphics.FromImage(bmp)) {
                Pen myPen = new Pen(Color.Red);
                PointF[] points = new PointF[data.Length];
                for (int i = 0; i < data.Length; i++) {
                    points[i].X = (float)i + i*2;
                    points[i].Y = data[i];
                }
                g.DrawLines(myPen, points);
                this.pictureBox1.Visible = true;
                this.pictureBox1.BackColor = Color.LightPink;
                this.pictureBox1.Width = 972;
                this.pictureBox1.Height = 454;
                this.pictureBox1.Image = bmp;
            }

        }
        #region 事件处理函数
        private void Open_Click(object sender, EventArgs e) {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            DialogResult openFileResult = openFileDialog.ShowDialog();
            string fileName = openFileDialog.FileName;
            if (openFileResult == DialogResult.OK) {
                if (!fileName.EndsWith(".seed")) {
                    DialogResult dialogResult = MessageBox.Show("本程序目前支持*.seed文件！", "是否继续？", MessageBoxButtons.OKCancel);
                    if (dialogResult == DialogResult.OK) {
                        openFileDialog.ShowDialog();
                    }
                    else {
                        return;
                    }
                }
                SEEDReader seedController = new SEEDReader(openFileDialog.FileName);
                this.SEED = seedController;
                Initialization(fileName);
            }
            // 
            //测试代码
            //float[] data = new float[1024];
            //Random random = new Random();
            //for (int num = 0; num < 4; num++) {
            //    int heigth = 100;
            //    for (int i = 0; i < data.Length; i++) {
            //        data[i] = (float)random.Next(0, 90) + heigth * num;
            //    }
            //    DrawWaveForm(data);
            //}
        }

        private void Initialization(string fileName) {
            this.tBoxSeedFileName.Text = fileName.Substring(fileName.LastIndexOf('\\') + 1);
            this.tCBox_Network.Items.AddRange(SEED.GetNetworkCodes());
            this.tCBox_Network.SelectedItem = this.tCBox_Network.Items[0];

            this.tCBoxStations.Items.AddRange(SEED.GetStationNameByNetCode(this.tCBox_Network.SelectedItem.ToString()));
            this.tCBoxStations.SelectedItem = this.tCBoxStations.Items[0];

            this.tCBoxChannels.Items.AddRange(SEED.GetChannelNameByStationName(this.tCBox_Network.SelectedItem.ToString(), this.tCBoxStations.SelectedItem.ToString()));
            pictureBox1.Image = null;
        }

        private void Exit_Click(object sender, EventArgs e) {
            this.Close();

        }
        #endregion

        private void tCBox_Network_SelectedIndexChanged(object sender, EventArgs e) {
            if (this.tCBox_Network.Items.Count != 0) {
                this.tCBoxStations.Items.Clear();
                this.tCBoxStations.Items.AddRange(SEED.GetStationNameByNetCode(this.tCBox_Network.SelectedItem.ToString()));
                this.tCBoxStations.SelectedItem = this.tCBoxStations.Items[0];
            }

        }

        private void tCBoxStations_SelectedIndexChanged(object sender, EventArgs e) {
            if (this.tCBoxStations.Items.Count != 0) {
                this.tCBoxChannels.Items.Clear();
                this.tCBoxChannels.Items.AddRange(SEED.GetChannelNameByStationName(this.tCBox_Network.SelectedItem.ToString(), this.tCBoxStations.SelectedItem.ToString()));
            }

        }

        private void tCBoxChannels_SelectedIndexChanged(object sender, EventArgs e) {
            if (this.tCBoxChannels.Items.Count != 0) {
                
                //GetWaveFormByNetCodeNStaName()
                //DrawWaveForm(data);
            }
        }
    }
}

